﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using PGI.EN.Inventario;

namespace PGI.AD.Inventario
{
    public class InventarioProductoAd
    {
        public static InventarioProductoEn SelProductoxCodBarras(string _codigoBarras)
        {
            SqlCommand command = null;
            SqlConnection connection = null;
            SqlDataReader reader = null;

            string sql = "MntProductoXCodBarraS";

            try
            {
                connection = DataBaseFactory.SqlOpenDatabase("PGI");

                command = new SqlCommand(sql, connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@CodBarra", _codigoBarras);

                reader = command.ExecuteReader();
                
                InventarioProductoEn InvProdEn = null;

                while (reader.Read())
                {
                    InvProdEn = new InventarioProductoEn();
                    InvProdEn.ProductoID = reader.IsDBNull(0) ? 0 : Convert.ToInt32(reader.GetValue(0));
                    InvProdEn.CategoriaID = reader.IsDBNull(1) ? 0 : Convert.ToInt32(reader.GetValue(1));
                    InvProdEn.CodigoProd = reader.IsDBNull(2) ? string.Empty : reader.GetValue(2).ToString();
                    InvProdEn.DescripcionProd = reader.IsDBNull(3) ? string.Empty : reader.GetValue(3).ToString();
                    InvProdEn.CodBarra = reader.IsDBNull(4) ? string.Empty : reader.GetValue(4).ToString();
                    InvProdEn.StockMinimo = reader.IsDBNull(5) ? 0 : Convert.ToInt32(reader.GetValue(5));
                    InvProdEn.DescripcionCat = reader.IsDBNull(6) ? string.Empty : reader.GetString(6);                    
                }

                return InvProdEn;
            }

            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                if (reader != null)
                    reader.Close();
                DataBaseFactory.SqlCloseDatabase("PGI");
            }
        }

        public static int InsertarRegistroInventario(int _productoID)
        {
            SqlCommand command = null;
            SqlConnection connection = null;
            SqlDataReader reader = null;

            string sql = "MntInventarioI";

            try
            {
                connection = DataBaseFactory.SqlOpenDatabase("PGI");

                command = new SqlCommand(sql, connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@ProductoID", _productoID);
                command.Parameters.Add("@Resultado", SqlDbType.Int).Direction = ParameterDirection.Output;

                command.ExecuteNonQuery();

                int resulInsInv = Convert.ToInt32(command.Parameters["@Resultado"].Value);


                return resulInsInv;
            }

            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                if (reader != null)
                    reader.Close();
                DataBaseFactory.SqlCloseDatabase("PGI");
            }
        }
    }
}
